// DlgFindRequest.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgFindRequest.h"


// CDlgFindRequest dialog

IMPLEMENT_DYNAMIC(CDlgFindRequest, CDialog)

CDlgFindRequest::CDlgFindRequest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgFindRequest::IDD, pParent)
	, m_nLibrary(0)
	, m_szFilter(_T(""))
	, m_szSearch(_T(""))
	, m_bCompleted(FALSE)
	, m_bILL(FALSE)
	, m_bOpen(FALSE)
	, m_bInProcess(FALSE)
	, m_bHeld(FALSE)
	, m_bLibrary(FALSE)
	, m_bNoILL(FALSE)
{
}

CDlgFindRequest::~CDlgFindRequest()
{
}

void CDlgFindRequest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_SEARCH, m_szSearch);
	DDX_Check(pDX, IDC_CHECK_COMPLETE_ONLY, m_bCompleted);
	DDX_Check(pDX, IDC_CHECK_ILL_ONLY, m_bILL);
	DDX_Check(pDX, IDC_CHECK_OPENED, m_bOpen);
	DDX_Check(pDX, IDC_CHECK_IN_PROCESS, m_bInProcess);
	DDX_Control(pDX, IDC_EDIT_SEARCH, m_ctlSearch);
	DDX_Check(pDX, IDC_CHECK_HELD_ONLY, m_bHeld);
	DDX_Check(pDX, IDC_CHECK_LIBRARY, m_bLibrary);
	DDX_Check(pDX, IDC_CHECK_NO_ILL, m_bNoILL);
	DDX_Control(pDX, IDC_DATETIMEPICKER_FIRST, m_ctlDateFirst);
	DDX_Control(pDX, IDC_DATETIMEPICKER_LAST, m_ctlDateLast);
}


BEGIN_MESSAGE_MAP(CDlgFindRequest, CDialog)
END_MESSAGE_MAP()


// CDlgFindRequest message handlers

BOOL CDlgFindRequest::OnInitDialog()
{
	CDialog::OnInitDialog();

	// disable ILL
	if( appConfig.m_Options.bDisableILL )
	{
		GetDlgItem( IDC_CHECK_NO_ILL )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_CHECK_ILL_ONLY )->ShowWindow( SW_HIDE );
	}

	// default to days
	if( appConfig.m_General.nDefaultDaysSearch > 0 )
	{
		COleDateTime dateDefault = COleDateTime::GetCurrentTime() - COleDateTimeSpan( appConfig.m_General.nDefaultDaysSearch, 0, 0, 0 );
		// set check
		m_ctlDateFirst.SetTime( dateDefault );
		m_ctlDateLast.SetTime();
	}
	else
	{
		// set check
		m_ctlDateFirst.SetTime();
		m_ctlDateLast.SetTime();
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgFindRequest::OnOK()
{
	// update data
	UpdateData();
	// build query string
	CString szQuery;
	CString szTemp;
	CString szSearch = m_szSearch;

	// escape search
	m_szSearch = appConfig.m_SQL.ES( m_szSearch );

	// build search
	if( !m_szSearch.IsEmpty() )
	{
		szQuery.Format( _T("(requests.request LIKE '%%%s%%' OR requests.information LIKE '%%%s%%' OR requests.notes LIKE '%%%s%%' OR requests.project LIKE '%%%s%%' OR requests.uid LIKE '%%%s%%') "),
			m_szSearch, m_szSearch, m_szSearch, m_szSearch, m_szSearch );
	}
	else if( m_ctlDateFirst.GetTime( CTime::GetCurrentTime() ) == GDT_NONE )
	{
		AfxMessageBox( _T("Please enter search criteria.") );
		return;
	}

	// get list of users
	if( !m_szSearch.IsEmpty() )
	{
		CArray<USER_SET> aUser;
		CString szFilter;
		// get users
		szFilter.Format( _T("(users.name LIKE '%%%s%%' OR users.email LIKE '%%%s%%' OR users.lan_id LIKE '%%%s%%' OR users.phone LIKE '%%%s%%' OR users.fax LIKE '%%%s%%') "), m_szSearch, m_szSearch, m_szSearch, m_szSearch, m_szSearch );
		appConfig.m_SQL.GetUserSet( &aUser, szFilter );
		// check for user
		if( aUser.GetCount() > 0 )
		{
			CString szUsers;
			if( !szQuery.IsEmpty() )
				szQuery += _T("OR ");
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			szQuery += szUsers;
		}
	}

	// get list of staff
	if( !m_szSearch.IsEmpty() )
	{
		CArray<STAFF_SET> aStaff;
		CString szFilter;
		// get users
		szFilter.Format( _T("(staff.name LIKE '%%%s%%' OR staff.email LIKE '%%%s%%' OR staff.phone LIKE '%%%s%%') "), m_szSearch, m_szSearch, m_szSearch );
		appConfig.m_SQL.GetStaffSet( &aStaff, szFilter );
		// check for user
		if( aStaff.GetCount() > 0 )
		{
			CString szStaff;
			if( !szQuery.IsEmpty() )
				szQuery += _T("OR ");
			for( INT nIndex = 0; nIndex < aStaff.GetCount(); nIndex++ )
			{
				if( !szStaff.IsEmpty() )
					szTemp.Format( _T(" OR requests.staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
				szStaff += szTemp;
			}
			szStaff += _T(") ");
			szQuery += szStaff;
		}
	}

	// check for ID
	if( !szSearch.IsEmpty() && IsNumeric( szSearch ))
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("OR ");
		szTemp.Format( _T("requests.id = %ld "), _tstol( szSearch ));
		szQuery += szTemp;
	}

	// held
	if( m_bHeld )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szTemp.Format( _T("(requests.status_id >= %d AND requests.status_id <= %d) "), STATUS_OFFSET_HOLD, STATUS_OFFSET_HOLD + 99 );
		szQuery += szTemp;
	}
	// completed
	if( m_bCompleted )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szTemp.Format( _T("requests.status_id = %d "), STATUS_ID_COMPLETE );
		szQuery += szTemp;
	}

	// ILL
	if( m_bNoILL )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		_T("requests.ill_id = 0 ");
	}
	else if( m_bILL )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		_T("requests.ill_id > 0 ");
	}
	
	// Open
	if( m_bOpen )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szQuery += _T("requests.touched > 0 ");
	}

	// In Progress
	if( m_bInProcess )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szTemp.Format( _T("requests.status_id = %d "), STATUS_ID_IN_PROCESS );
		szQuery += szTemp;
	}
	// current library
	if( m_bLibrary )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szQuery += _T("requests.atlibrary > 0 ");
	}

	CTime time;
	// get date start
	if( m_ctlDateFirst.GetTime( time ) != GDT_NONE )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szTemp.Format( _T("requests.date_in >= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ));
		szQuery += szTemp;
	}

	// get date end
	if( m_ctlDateLast.GetTime( time ) != GDT_NONE )
	{
		if( !szQuery.IsEmpty() )
			szQuery += _T("AND ");
		szTemp.Format( _T("requests.date_in <= '%s' "), time.Format( _T("%Y-%m-%d 23:59:59") ));
		szQuery += szTemp;
	}

	// set query string
	m_szFilter = szQuery;

	CDialog::OnOK();
}

BOOL CDlgFindRequest::IsNumeric(CString& str)
{
	int nLen = str.GetLength();

	INT nIndex;
	for( nIndex = 0; nIndex < nLen; nIndex++ )
		if( !_istdigit( str[nIndex] )) break;

	return nIndex == nLen; // will be "TRUE" if all characters passed the test
}
